<?php

namespace app\admin\controller;

use app\common\controller\Send;
use app\common\enums\StatusEnum;
use think\facade\Request;
use app\admin\model\LangClassReciteType AS TypeModel;
use app\admin\validate\LangClassReciteType AS TypeValidate;
use app\admin\model\LangClassRecite;
use app\admin\model\LangClassFile;

class LangClassReciteType extends Base
{
    use Send;

    /**
     * 分类列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $search = input();
        $classId = isset($search['classId']) ? $search['classId'] : 0;
        $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
        $type = isset($search['type']) ? $search['type'] : LangClassRecite::TYPE_WORD;
        $lang = isset($search['lang']) ? $search['lang'] : '';
        $className = isset($search['className']) ? $search['className'] : '';
        $name = isset($search['name']) ? $search['name'] : '';
        $status = isset($search['status']) ? $search['status'] : -1;

        $search = [
            'lang' => $lang,
            'className' => $className,
            'classId' => $classId,
            'courseType' => $courseType,
            'type' => $type,
            'name' => $name,
            'status' => $status
        ];

        $where = [];
        if ($search['name']) {
            $where[] = ['name', 'like', '%' . $search['name'] . '%'];
        }

        if ($search['status'] != null && $search['status'] != -1) {
            $where[] = ['status', '=', $search['status']];
        }

        $model = TypeModel::getList($classId, $courseType, $type, $where);
        $title = ($type == LangClassRecite::TYPE_WORD) ? '单词' : '句子';
        return view('index', [
            'title' => $title,
            'model' => $model['model'],
            'count' => $model['count'],
            'search' => $search,
            'status' => StatusEnum::$listExplain,
            'courseName' => LangClassFile::attributes('course_type', $courseType),
            'typeName' => LangClassRecite::attributes('type', $type),
        ]);
    }

    /**
     * 添加修改
     * @return \think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function form()
    {
        if (Request::instance()->isPost()) {
            $data = input('post.');

            // 检测变量
            $validate = new TypeValidate();
            if (!$validate->check($data)) {
                return json(self::sendResult(102, [], $validate->getError()));
            }

            $langId = TypeModel::saveOne($data);
            $code = 602;
            if ($langId > 0) {
                $code = self::$okCode;
            }

            return json(self::sendResult($code, [
                'url' => url('index', [
                    'classId' => $data['class_id'],
                    'courseType' => $data['course_type'],
                    'type' => $data['type'],
                    'lang' => $data['lang'],
                    'className' => $data['className'],
                ]),
            ]));
        } else {
            $search = input();
            $lang = isset($search['lang']) ? $search['lang'] : '';
            $className = isset($search['className']) ? $search['className'] : '';
            $classId = isset($search['classId']) ? $search['classId'] : 0;
            $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
            $type = isset($search['type']) ? $search['type'] : 0;
            $id = isset($search['id']) ? $search['id'] : 0;

            $model = [];
            $title = ($type == LangClassRecite::TYPE_WORD) ? '单词套题' : '句子套题';
            if ($id > 0) {
                $title = '编辑' . $title;
                $model = TypeModel::get($id);
            } else {
                $title = '添加' . $title;
            }
            return view('form', [
                'status' => StatusEnum::$listExplain,
                'model' => $model,
                'title' => $title,
                'lang' => $lang,
                'className' => $className,
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
            ]);
        }
    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function delete($id)
    {
        $search = input();
        $lang = isset($search['lang']) ? $search['lang'] : '';
        $className = isset($search['className']) ? $search['className'] : '';
        $classId = isset($search['classId']) ? $search['classId'] : 0;
        $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
        $type = isset($search['type']) ? $search['type'] : 0;
        TypeModel::deleteOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }

    public function deletes()
    {
        $search = input();
        $lang = isset($search['lang']) ? $search['lang'] : '';
        $className = isset($search['className']) ? $search['className'] : '';
        $classId = isset($search['classId']) ? $search['classId'] : 0;
        $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
        $type = isset($search['type']) ? $search['type'] : 0;
        $ids = $search['ids'];

        // 删除数据
        TypeModel::deleteAll($ids);
        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }

    /**
     * 修改状态
     * @param $id
     * @return array
     */
    public function disabled($id)
    {
        $search = input();
        $lang = isset($search['lang']) ? $search['lang'] : '';
        $className = isset($search['className']) ? $search['className'] : '';
        $classId = isset($search['classId']) ? $search['classId'] : 0;
        $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
        $type = isset($search['type']) ? $search['type'] : 0;

        TypeModel::disabledOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }

    /**
     * 批量启用、禁用
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function disables()
    {
        $search = input();
        $lang = isset($search['lang']) ? $search['lang'] : '';
        $className = isset($search['className']) ? $search['className'] : '';
        $classId = isset($search['classId']) ? $search['classId'] : 0;
        $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
        $type = isset($search['type']) ? $search['type'] : 0;

        $status = $search['status'];
        $ids = $search['ids'];
        TypeModel::disables($ids, $status);
        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }
}
